import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1773. 统计匹配检索规则的物品数量
 * https://leetcode-cn.com/problems/count-items-matching-a-rule/
 */
public class Solutions_1773 {
    public static void main(String[] args) {
//        List<List<String>> items = new ArrayList<>();
//        items.add(Arrays.asList("phone", "blue", "pixel"));
//        items.add(Arrays.asList("computer", "silver", "lenovo"));
//        items.add(Arrays.asList("phone", "gold", "iphone"));
//        String ruleKey = "color",  ruleValue = "silver";  // output: 1

        List<List<String>> items = new ArrayList<>();
        items.add(Arrays.asList("phone", "blue", "pixel"));
        items.add(Arrays.asList("computer", "silver", "phone"));
        items.add(Arrays.asList("phone", "gold", "iphone"));
        String ruleKey = "type",  ruleValue = "phone";  // output: 2

        int result = countMatches(items, ruleKey, ruleValue);
        System.out.println(result);
    }

    public static int countMatches(List<List<String>> items, String ruleKey,
                                   String ruleValue) {
        int idx = 0;
        // 根据 ruleKey 确定获取元素的索引
        if ("color".equals(ruleKey)) {
            idx = 1;
        } else if ("name".equals(ruleKey)) {
            idx = 2;
        }
        int res = 0;
        for (List<String> item : items) {
            if (ruleValue.equals(item.get(idx))) {
                // 是否匹配
                res ++;
            }
        }
        return res;
    }
}
